package fr.cephb.joperon.tool.stypesgui;

import fr.cephb.joperon.core.bio.AcidNucleicUtil;
import fr.cephb.joperon.structures.STypes;
import fr.cephb.util.JSONBuilder;
import fr.cephb.util.JSONizable;

/**
 * Genotype
 * @author pierre
 *
 */
public class Genotype
	implements JSONizable
	{
	
	private STypes first;
	private STypes second;
	private DataSource raw=null;
	private DataSource corrected=null;
	Genotype(STypes first,STypes second)
		{
		this.first=first;
		this.second=second;
		}
	
	public class DataSource
		implements JSONizable
		{
		STypes stype;
		
		private DataSource(STypes stype)
			{
			this.stype=stype;
			}
		
		public int getA()
			{
			return this.stype.a * first.i;
			}

		public int getC()
			{
			return this.stype.c * first.i;
			}

		public int getG()
			{
			return this.stype.g * first.i;
			}

		public int getN()
			{
			return this.stype.n * first.i;
			}

		public int getT()
			{
			return this.stype.t * first.i;
			}
		@Override
		public String toJSON()
			{
			return new JSONBuilder().
			put("A", getA()).
			put("C", getC()).
			put("G", getG()).
			put("T", getT()).
			put("N", getN()).
			getString();
			}
		@Override
		public String toString()
			{
			return toJSON();
			}
		}

	public DataSource getRaw()
		{
		if(raw==null) raw= new DataSource(this.first);
		return raw;
		}
	
	public DataSource getCorrected()
		{
		if(corrected==null) corrected= new DataSource(this.second);
		return corrected;
		}
	
	
	public int getA()
		{
		return getCorrected().getA();
		}

	public int getC()
		{
		return getCorrected().getC();
		}

	public int getG()
		{
		return getCorrected().getG();
		}

	public int getN()
		{
		return getCorrected().getN();
		}

	public int getT()
		{
		return getCorrected().getT();
		}
	
	public int getDeletions()
		{
		return first.d * first.i;
		}
		
	public int getInsertions()
		{
		return first.f * first.i;
		}
	
	public String getAlleles()
		{
		return AcidNucleicUtil.decode_genotype(second.n);
		}
		
	public int getLog10()
		{
		return second.i;
		}
		
	public int getCoverage()
		{
		return second.f * first.i;
		}
	
	@Override
	public String toJSON()
		{
		return new JSONBuilder().
			put("raw", getRaw()).
			put("corrected", getCorrected()).
			put("log10", getLog10()).
			put("coverage", getCoverage()).
			put("inserts", getInsertions()).
			put("deletions", getDeletions()).
			getString();
		}
		
	@Override
	public String toString()
		{
		return toJSON();
		}
	}
